<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\SmsLog;
use App\User;
use Illuminate\Support\Str;
use phpDocumentor\Reflection\Types\Integer;

class BaseController extends Controller
{

    /**全站返回json
     * @param $code
     * @param $msg
     * @param $ata
     * @return \Illuminate\Http\JsonResponse
     */
    public function ResultJson(int $code = 0, string $msg = '', array $ata)
    {
        $res = [
            'code' => $code,
            "msg" => $msg,
            'data' => $ata
        ];
        return response()->json($res);
    }

    /**
     * @param int $phone
     * @return void
     * 获取验证码
     */
    public function getCode(int $phone)
    {
        $SmsLog = new SmsLog();
        $res = $SmsLog->checkRate($phone);
        if (!empty($res)) {
            return $res['code'] ?? null;
        }
        return null;
    }

    /**
     * 更新验证码设置失效
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function SetCode(int $phone, int $code)
    {
        $SmsLog = new SmsLog();
        return $SmsLog->updateCode($phone, $code);
    }

    /*
     * 查询当前用户是否存在
     */
    protected function IssetUser(int $phone)
    {
        return User::where('phone', $phone)->first();
    }

    /**更新用户信息
     * @param array $data
     * @return void
     */
    public function UpdateUser(array $data, int $phone)
    {
        return User::update($data)->where(['phone' => $phone]);
    }
}
